#include "libs/string.h"
#include "kernel/log/log.h"

String::String(char* str/* args */)
{
    char* temp = str;
    memset(data,0,STRING_MAX_LENGTH);
    charList = NULL;    
    count = 0;
    while(*temp){

        count++;
        temp++;

    }

    if(count > STRING_MAX_LENGTH)
        return;
        
    memcpy(str,data,count);

}

String::~String()
{

    if(charList)
    {
        for (size_t i = 0; i < index; i++)
        {
            /* code */
            delete charList[i];

        }
        
        delete charList;
    }

}

char** String::split(char iterator)
{
    if(charList)
    {
        return charList;
    }
    uint64_t max = 10;
    charList =  new char*[max];
    memset(charList,0,max * sizeof(char*));

   index = 0;
   uint64_t tempCount = 0;
    
    for (size_t i = 0; i < count; i++)
    {

        if(data[i] == iterator || (i == (count -1) ))
        {
            if(!tempCount)
                continue;
            char* tempChar =  new char[tempCount + 1];
            memset(tempChar,0,tempCount + 1);
            memcpy(&data[i - tempCount],tempChar,tempCount);
            charList[index++] = tempChar;
            tempCount = 0;
            if(index >= max){
                char** tempCharList = new char*[max+10];
                max += 10;
                memset(tempCharList,0,max * sizeof(char*));
                memcpy(charList,tempCharList,index * sizeof(char*));
                delete charList;
                charList = tempCharList;
            }
            continue;
        }
        
        tempCount++;


    }

    if(index != max){

        char** tempCharList = new char*[index];
        memset(tempCharList,0,(index) * sizeof(char*));
        memcpy(charList,tempCharList,(index) * sizeof(char*));
        delete charList;
        charList = tempCharList;

    }

    return charList;
    
}

char* String::charBuffer()
{


    

    return data;    
}
